package net.dlz.example.controller;

import net.dlz.example.domain.District;
import net.dlz.example.service.DistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/District")
public class DistrictController {
    @Autowired
    private DistrictService districtService;

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public String districtPage(Model model){
        List<District> districts = districtService.listdistrict();
        model.addAttribute("districts",districts);
        return "district-list";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String addDistrictPage(Model model){
        model.addAttribute("district", new District());
        return "district-add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String saveDistrict(District district){
        int updated = districtService.saveDistrict(district);
            return "redirect:/District/search";
    }

    @GetMapping(value = "/edit")
    public String editDistrictPage(@RequestParam("id") String id, Model model){
        District district = districtService.findDistrictByPrimaryKey(id);
        model.addAttribute("district",district);
        return "district-add";
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteDistrict(@RequestParam("id") String id){
        int deleted = districtService.deleteDistrictByPrimaryKey(id);
        return deleted > 0;
    }

    @RequestMapping("/districtTypeCount")
    public String selectDistrictTypeCount(String districtName,Model model){
        List<District> districtList =districtService.selectDistrictTypeCount(districtName);
        model.addAttribute("district",districtList);
        return "statistics";
    }

    @RequestMapping("/findAllDistrict")
    @ResponseBody
    public List<District> findDistrict(){
        List<District> districts =districtService.findDistrictByName();
        return districts;
    }

}
